﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;
using devtm.Helpers;
using System.ComponentModel;
using devtm.Collections;


namespace devtm.Editor.Configuration.Customs
{

    public class CustomType : INotifyPropertyChanged
    {

        public CustomType() 
        {

        }


        #region Generate New Item


        public void Create(Type type, string path)
        {

            CustomTemplate p;

            if (Templates.ContainsKey(path))
                p = Templates[path];
            else
            {
                p = new CustomTemplate()
                {
                    Path = path,
                    LastUpdate = DateTime.Now
                };

                p.PropertyChanged += c_PropertyChanged;

                p.Create(type);

                Templates.Add(p);

            }

        }

        #endregion

        #region Properties

        public string Fullname { get; set; }


        private DateTime _LastUpdate;
        [System.Xml.Serialization.XmlAttribute()]
        public DateTime LastUpdate
        {
            get
            {
                return _LastUpdate;
            }
            set
            {
                if (_LastUpdate == value)
                    return;
                _LastUpdate = value;

                PropertyChange("LastUpdate");
            }
        }


        public IndexedObservableCollection<CustomTemplate, string>
            Templates = new IndexedObservableCollection<CustomTemplate, string>
                (c => c.Path);


        #endregion

        #region Change

        private void PropertyChange(string p)
        {

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));

            if (p != "LastUpdate")
                LastUpdate = DateTime.Now;

        }

        void c_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs f)
        {

            if (f.PropertyName == "LastUpdate")
                LastUpdate = (sender as CustomTemplate).LastUpdate;

        }

       
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
